package com.jyx.refactor.method;

import java.util.Enumeration;
import java.util.Vector;

import com.jyx.refactor.beginner.service.Price;

/**
 * Extract Method 提炼函数
 * author 		jyx
 * email 		jyxsuper@163.com
 * description: ExtractMethod
 * 				将这段代码放到一个独立函数中，并让函数名称解释该函数的用途
 * Company: 	myzczk
 * datetime: 	2017年9月22日 上午10:18:21
 */
public class ExtractMethod {

	/** 名称 */
	private static String _name;
	/** 订单集合 */
	private static Vector _orders = new Vector();
	
	/**
	 * extract method
	 * 提炼函数==>resource
	 * @param outstanding
	 */
	public static void printOwing(double outstanding){
		Enumeration e = _orders.elements();
		// print banner
		System.out.println("*******************************");
		System.out.println("********* Custoer Owes ********");
		System.out.println("*******************************");
		// calculate outstanding
		while (e.hasMoreElements()) {
			Price price = (Price) e.nextElement();
			outstanding += price.calcAmount(1);
		}
		
		// pring detail
		System.out.println("name:"+_name);
		System.out.println("amount:"+outstanding);
	}
	
	/**
	 * extract method
	 * 提炼函数==>target
	 * @param outstanding
	 */
	public void printOwingTarget(double outstanding){
		printBanner();
		outstanding = getOutstanding(outstanding);
		printDetail(outstanding);
	}
	/**
	 * calculate outstanding
	 * @return
	 */
	private double getOutstanding(double outstanding) {
		Enumeration e = _orders.elements();
		while (e.hasMoreElements()) {
			Price price = (Price) e.nextElement();
			outstanding += price.calcAmount(1);
		}
		return outstanding;
	}

	/**
	 * print banner
	 */
	public void printBanner(){
		System.out.println("*******************************");
		System.out.println("********* Custoer Owes ********");
		System.out.println("*******************************");
	}
	
	/**
	 * pring detail
	 * @param outstanding
	 */
	public void printDetail(double outstanding){
		System.out.println("name:"+_name);
		System.out.println("amount:"+outstanding);
	}
	
}
